/**
 * Copyright ©2012-2018 HopeHaiyang Corporation, All Rights Reserved
 * http://www.wuhanhope.com
 */
package hope.platform.data.web;

import hope.platform.data.domain.Warning;
import hope.platform.data.server.data.header.WarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * class info
 *
 * @author hujia
 *         Create at 2018/7/20 16:05
 */
@RestController
@RequestMapping("/Warning")
public class WarningController {
    @Autowired
    private WarningService warningService;

    @RequestMapping(method = RequestMethod.POST)
    public Warning addWarning(@RequestBody Warning warning) throws Exception {
        return warningService.addWarning(warning);
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Warning> getAll() throws Exception {
        return warningService.findAll();
    }

    @RequestMapping(value = "/serialNumber/{serialNumber}", method = RequestMethod.GET)
    public List<Warning> getBySerialNumber(@PathVariable String serialNumber) throws Exception {
        return warningService.findBySerialNumber(serialNumber);
    }

    @RequestMapping(value = "/id/{id}", method = RequestMethod.GET)
    public Warning getById(@PathVariable String id) throws Exception {
        return warningService.findById(id);
    }

    @RequestMapping(value = "/count/serialNumber/{serialNumber}/dataName/{dataName}" +
            "/beginTime/{beginTime}/endTime/{endTime}",method = RequestMethod.GET)
    public long countBySerialNumberAndDataNameAndGmtCreateBetween(
            @PathVariable String serialNumber,
            @PathVariable String dataName,
            @PathVariable String beginTime,
            @PathVariable String endTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date beginDate = sdf.parse(beginTime);
        Date endDate = sdf.parse(endTime);
        return warningService.countBySerialNumberAndDataNameAndGmtCreateBetween(
                serialNumber, dataName, beginDate, endDate);
    }

    @RequestMapping(value = "/all/beginTime/endTime/{beginTime}/{endTime}", method = RequestMethod.GET)
    public List<Warning> getByGmtCreateTimeBetween(@PathVariable String beginTime,
                                                         @PathVariable String endTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date beginDate = sdf.parse(beginTime);
        Date endDate = sdf.parse(endTime);
        return warningService.getByGmtCreateBetween(beginDate,endDate);
    }
}
